package com.ztsoft.user.controller.dynamic;

import com.ztsoft.user.common.base.BaseController;
import com.ztsoft.user.common.exception.CustomException;
import com.ztsoft.user.pojo.entity.dynamic.ColumnDescription;
import com.ztsoft.user.pojo.enums.ResultCode;
import com.ztsoft.user.pojo.vo.dynamic.CustomColumnVo;
import com.ztsoft.user.pojo.vo.dynamic.RecordsVo;
import com.ztsoft.user.service.dynamic.ColumnDescriptionService;
import com.ztsoft.user.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RequestMapping("/columnDescription")
@RestController
@Api(tags = "表格列表模块")
@CrossOrigin("*")
public class ColumnDescriptionController extends BaseController<ColumnDescription, String, ColumnDescriptionService> {

    @PostMapping("/saveColumnDescription")
    @ApiOperation(value = "添加模板或用户自定义动态表格列")
    public Result<Object> saveColumnDescription(@RequestBody ColumnDescription columnDescription) {
        baseService.saveColumnDescription(columnDescription);
        return Result.success();
    }

    @GetMapping("/queryColumnByTableCode")
    @ApiOperation(value = "显示表格列")
    public Result<Object> queryColumnByTableCode(ColumnDescription columnDescription) {
        return Result.success(baseService.queryColumnByTableCode(columnDescription));
    }

    @PutMapping("/update")
    @ApiOperation(value = "修改表格列")
    public Result<Object> updateColumnDescription(@RequestBody ColumnDescription columnDescription) {
        try {
            Date date = new Date();
            columnDescription.setModifyTime(date);
            baseService.updateColumnDescription(columnDescription);
            return Result.success();
        } catch (Exception e) {
            throw new CustomException(ResultCode.ERROR.getMsg(), ResultCode.ERROR.getCode());
        }
    }

    //修改多条记录(批量修改)
    @PutMapping("/updateList")
    @ApiOperation(value = "批量修改表格列")
    public Result<Object> updateList(@RequestBody List<ColumnDescription> list, String departmentOrRoleId) {
        try {
            Date date = new Date(System.currentTimeMillis());
            for (ColumnDescription columnDescription : list) {
                columnDescription.setModifyTime(date);
                columnDescription.setDeptId(departmentOrRoleId);
                baseService.updateById(columnDescription);
            }
            return Result.success();
        } catch (Exception e) {
            throw new CustomException(ResultCode.ERROR.getMsg(), ResultCode.ERROR.getCode());
        }
    }

    //获取id和表头名
    @GetMapping("/queryIdAndTitle")
    @ApiOperation(value = "获取id和表头名")
    public Result<Object> queryIdAndTitle(ColumnDescription columnDescription) {
        List<ColumnDescription> list = baseService.queryIdAndTitle(columnDescription);
        return Result.success(list);
    }

    //自定义列表
    @GetMapping("/queryCustomColumnList/{tableCode}")
    @ApiOperation(value = "自定义列表")
    public Result<Object> queryCustomColumnList(@PathVariable String tableCode) {
        Map<String, Object> resultMap = new HashMap<>();
        List<CustomColumnVo> list = baseService.queryCustomColumnList(tableCode);
        resultMap.put("data", list);
        resultMap.put("size", list.size());
        return Result.success(resultMap);
    }

    //用户自定义列保存
    @PostMapping("/saveCustomColumn/{tableCode}")
    @ApiOperation(value = "自定义列保存")
    public Result<Object> saveCustomColumn(@RequestBody RecordsVo recordsVo, @PathVariable("tableCode") String tableCode) {
        List<CustomColumnVo> list = recordsVo.getData().getUpdateRecords();
        baseService.saveCustomColumn(list, tableCode);
        return Result.success();
    }
}
